using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimeSheetBLL;

namespace TimeSheet.Protected
{
    /// <summary>
    /// Summary description for StaffReport.
    /// </summary>
    public class StaffReport : Page
    {
        protected Label lblTitle;
        protected Label lblDateRange;
        protected Label lblTotalBillable;
        protected Label lblNonBillableTitle;
        protected Label lblTotalHours;
        protected DataGrid dgBillable;
        protected DataGrid dgNonBillable;
        protected Label lblClaimedOvertime;
        protected DataGrid dgOvertime;
        protected Label lblOvertime;
        protected Label lblTotalNonBillable;

        private void Page_Load(object sender, EventArgs e)
        {
            User user = GetUser();
            lblTitle.Text = "Hours Report for <u>" + user.FullName + "</u>";

            lblDateRange.Text = "From " + GetFromDate().ToShortDateString() +
                                " to " + GetToDate().ToShortDateString();

            SetTotals(user);
            if (GetShowNonBillable())
            {
                dgNonBillable.Visible = true;
                lblNonBillableTitle.Visible = true;
                BindNonBillableHours(user);
            }

            if (GetShowOvertime())
            {
                dgOvertime.Visible = true;
                lblOvertime.Visible = true;
                lblClaimedOvertime.Visible = true;
                BindOvertimeHours(user);
            }

            BindBillableHours(user);
        }

        private void BindOvertimeHours(User user)
        {
            DataTable table =
                user.GetOvertimeHours(GetFromDate(), GetToDate(), AppSettings.HoursInDay);
            dgOvertime.DataSource = table;
            dgOvertime.DataBind();

            double overtime = 0;
            foreach (DataRow r in table.Rows)
            {
                overtime += Convert.ToDouble(r["claimed_hours"]);
            }
            lblOvertime.Text = overtime.ToString("F");
        }

        private void BindBillableHours(User user)
        {
            dgBillable.DataSource = user.GetProjectHours(GetFromDate(), GetToDate());
            dgBillable.DataBind();
        }

        private void BindNonBillableHours(User user)
        {
            dgNonBillable.DataSource = user.GetActivityHours(GetFromDate(), GetToDate());
            dgNonBillable.DataBind();
        }

        private void SetTotals(User user)
        {
            double billable = user.GetTotalChargeableHours(GetFromDate(), GetToDate());
            double nonBillable =
                user.GetTotalNonChargeableHours(GetFromDate(), GetToDate());
            double total = billable + nonBillable;

            lblTotalBillable.Text = billable.ToString("F");
            lblTotalNonBillable.Text = nonBillable.ToString("F");
            lblTotalHours.Text = total.ToString("F");
        }

        private User GetUser()
        {
            User user = new User();
            user.ID = Convert.ToInt64(Server.UrlDecode(Request["id"]));
            BrokerFactory.Fill(user);
            return user;
        }

        private DateTime GetFromDate()
        {
            return Convert.ToDateTime(Server.UrlDecode(Request["from"]));
        }

        private DateTime GetToDate()
        {
            return Convert.ToDateTime(Server.UrlDecode(Request["to"]));
        }

        private bool GetShowNonBillable()
        {
            return Convert.ToBoolean(Server.UrlDecode(Request["nonbillable"]));
        }

        private bool GetShowOvertime()
        {
            return Convert.ToBoolean(Server.UrlDecode(Request["overtime"]));
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}